package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.qyxx.entity.BgSjmkjgOrder;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.mapper.BgSjmkjgOrderMapper;
import org.jeecg.modules.qyxx.service.IFkPdfService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.pdf.HtmlToPdf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.UUID;

@Service
// @DS("multi-datasource1")申报
public class FkPdfServiceImpl implements IFkPdfService {
    private static final String HTML_URL_PATH = "http://10.10.18.5:5501/src/views/PDFhtml/WEB-INF/dhcsfxbg.html";

    @Autowired
    private BgSjmkjgOrderMapper bgSjmkjgOrderMapper;

    @Override
    public String getReportPdf(BgNsrjbxxReq req) {
        // TODO 后期需要放开
        BgSjmkjgOrder bgSjmkjgOrder = bgSjmkjgOrderMapper.getReportPdfUrl(req);
        /*String reportPdfUrl = bgSjmkjgOrder.getPdfUrl();
        if (StringUtils.hasLength(reportPdfUrl)) {
            File file = new File(reportPdfUrl);
            //如果pdf存在，则直接返回
            if (file.exists()) {
                return reportPdfUrl;
            }
        }*/
        DateUtils utils = DateUtils.getInstance();
        String htmlUrl = HTML_URL_PATH + "?data=" + req.getDjxh() + "_" + utils.format(req.getJssj()) + "_" + utils.format(req.getKssj());
        System.out.println(htmlUrl);
        String pdfName = UUID.randomUUID().toString() + ".pdf";
        // TODO 到时候路径需要改成Linux服务器的
        // 文件所存放的地址 我这边放在D盘下
        String path = "/data/foresee/service/fk-system/pdf";
        // PDF存放的路径
        String destPath = path + "/" + pdfName;
        // 通过html生成PDF
        boolean flag = HtmlToPdf.convert(htmlUrl, destPath);
        File file = new File(destPath);
        //如果pdf不存在，则生成失败
        if (!file.exists()) {
            return null;
        }
        String newDestPath = null;
        try {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            StringBuffer waterMarkName = new StringBuffer();
            waterMarkName.append("中国建设银行 ").append(sysUser.getRealname()).append("(").append(sysUser.getPost()).append(")");
            newDestPath = path + "/" + UUID.randomUUID().toString() + ".pdf";
            HtmlToPdf.setWaterMarkForPDF(destPath, newDestPath, waterMarkName.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            bgSjmkjgOrder.setPdfUrl(newDestPath);
            LambdaUpdateWrapper<BgSjmkjgOrder> wrapper = Wrappers.lambdaUpdate(BgSjmkjgOrder.class);
            wrapper.eq(BgSjmkjgOrder::getDjxh, req.getDjxh()).eq(BgSjmkjgOrder::getJssj, req.getJssj()).eq(BgSjmkjgOrder::getKssj, req.getKssj());
            bgSjmkjgOrderMapper.update(bgSjmkjgOrder, wrapper);
            return newDestPath;
        } else {
            return null;
        }
    }
}
